﻿namespace Web
{
    using System;
    using System.Collections;
    using System.Web.UI;
    using Cloudio.Web.UI.Ide;
    using Cloudio.Web.UI.SourceViewer;
    using Gaia.WebWidgets;
    using Gaia.WebWidgets.Extensions;

    public partial class Default : Page
    {
        private SourceViewer _sourceBase;
        private const string SolutionFolder = "~/files/";

        protected void Page_Load(object sender, EventArgs e)
        {
            //open as default
            if (!IsPostBack)
                OpenFiles.Add(SolutionFolder + "class1.cs");

            AddSolutionExplorer();
            AddSourceViewers();
        }

        private void AddSourceViewers()
        {
            tabSource.Controls.Clear();
            foreach (var path in OpenFiles)
                AddSourceViewerTab(path.ToString());

            tabSource.Visible = tabSource.Controls.Count != 0;
        }

        private void AddSourceViewerTab(string path)
        {
            _sourceBase = new SourceViewer(path);

            _sourceBase.CloseClicked += delegate
                                            {
                                                OpenFiles.Remove(path);
                                                AddSourceViewers();
                                                vportRight.FirstView.ForceAnUpdate();

                                                //TODO set activetabindex
                                            };
            
            var tabView = new TabView {Caption = path};
            tabView.Controls.Add(_sourceBase);

            tabSource.TabViews.Add(tabView);
        }

        public ArrayList OpenFiles
        {
            get
            {
                if (ViewState["OpenFiles"] == null)
                    ViewState["OpenFiles"] = new ArrayList();

                return (ArrayList)ViewState["OpenFiles"];
            }
        }

        private void AddSolutionExplorer()
        {
            var solutionExplorer = new SolutionExplorer {ID = "expl"};
            solutionExplorer.TreeViewNodeSelected += solutionExplorer_TreeViewNodeSelected;
            vportRight.SecondView.Controls.Add(solutionExplorer);
        }

        void solutionExplorer_TreeViewNodeSelected(object sender, EventArgs e)
        {
            //get id from the expecpted LinkButton
            var filename = ((LinkButton) sender).ID;

            string path = SolutionFolder + filename;

            //if already exist, just make sure it is selected
            if (OpenFiles.Contains(path))
            {
                tabSource.ActiveTabViewIndex = (short) OpenFiles.IndexOf(path);
                return;
            }

            OpenFiles.Add(path);
            AddSourceViewerTab(path);

            //set right tabview to show
            tabSource.ActiveTabViewIndex = (short) (tabSource.Controls.Count - 1);

            vportRight.FirstView.ForceAnUpdate();
        }
    }
}